package com.trainfinder.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public abstract class NewAbstractRemoteRunnable implements Runnable {

	private final Logger logger;

	private final int uniqueIndex;

	protected NewAbstractRemoteRunnable(Logger logger, int uniqueIndex) {

		this.logger = logger;
		this.uniqueIndex = uniqueIndex;
	}

	public void run() {

		Service service = getService();

		try {

			final InputStream input = service.getInputStream();
			final OutputStream output = service.getOutputStream();

			// StringBuffer sb = new StringBuffer();
			try {
				int value;
				while  ((value = input.read()) != -1) {
					 
					output.write(value);
				//	System.out.print((char)value);
					logger.log(PortType.Remote, uniqueIndex,
							new byte[] { (byte)value }, 1);
				} 

			} catch (IOException e) {
	//			e.printStackTrace();
				 
			}
			//output.flush();
			finally {
				output.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServerCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected abstract Service getService();

}
